package destiny.music.view;

import java.text.DecimalFormat;

import android.R;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import destiny.music.entity.Music;

public class MusicListItem extends LinearLayout{
	
	public static final DecimalFormat decimalFormat = new DecimalFormat("0.00MB");
	public static final float MB = 1024*1024f;
	
	CheckBox check;
	TextView name;
	TextView size;
	Button down;
	Music music;
	TextView singer;
	TextView liupai;
	
	public MusicListItem(Context context,Music m) {
		super(context);
		this.setLayoutParams(new ListView.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
		this.setGravity(Gravity.FILL_HORIZONTAL);
		this.setOrientation(LinearLayout.VERTICAL);
		
		this.music = m;
		
		this.addView(createRow1());
		this.addView(createLine());
		this.addView(createRow2());
	}
	
	public LinearLayout createRow1(){
		LinearLayout linearLayout =  new LinearLayout(getContext());
		linearLayout.setLayoutParams(new ListView.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
		linearLayout.setGravity(Gravity.CENTER_VERTICAL);
		linearLayout.setOrientation(LinearLayout.HORIZONTAL);
		
		name = new TextView(getContext());
		name.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,1));
		name.setTextAppearance(getContext(), R.attr.textAppearanceSmall);
		name.setTextSize(12);
		name.setText(music.getName());

		
		down = new Button(getContext(),null,R.attr.buttonStyleSmall);
		down.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		down.setTextSize(12);
		down.setHeight(12);
		down.setText("down");
		down.setOnClickListener(new ActionListener());
		
		linearLayout.addView(name);
		linearLayout.addView(down);
		
		return linearLayout;
	}
	
	public View createLine(){
		TextView view = new TextView(getContext());
		view.setLayoutParams(new ListView.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
		view.setHeight(1);
		return view;
	}
	
	public LinearLayout createRow2(){
		LinearLayout linearLayout =  new LinearLayout(getContext());
		linearLayout.setLayoutParams(new ListView.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
		linearLayout.setGravity(Gravity.CENTER_VERTICAL);
		linearLayout.setOrientation(LinearLayout.HORIZONTAL);
		
		singer = new TextView(getContext());
		singer.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,0.5f));
		singer.setTextAppearance(getContext(), R.attr.textAppearanceSmall);
		singer.setText("演唱：未知");
		
		liupai = new TextView(getContext());
		liupai.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,0.5f));
		liupai.setTextAppearance(getContext(), R.attr.textAppearanceSmall);
		liupai.setText("流派：未知");
		
		size = new TextView(getContext());
		size.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		size.setTextAppearance(getContext(), R.attr.textAppearanceSmall);
		size.setText(decimalFormat.format(music.getSize()/MB));
		
		linearLayout.addView(singer);
		linearLayout.addView(liupai);
		linearLayout.addView(size);
		
		return linearLayout;
	}
	
	public Music getMusic() {
		return music;
	}
	public void setMusic(Music music) {
		
	}
	
	class ActionListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			if(v instanceof Button){
				Button b = (Button) v;
				if(b.getText().equals("down"))
					System.out.println("down :"+music);
			}
		}
		
	}

}
